package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.mapper.YddAdvimgMapper;
import com.itheima.ydd.service.IYddAdvimgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.service.IYddAdvposService;
import com.itheima.ydd.vo.YddAdvimgVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
public class YddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper, YddAdvimg> implements IYddAdvimgService {
    @Autowired
    private IYddAdvposService yddAdvposService;

    @Override
    public Page<YddAdvimgVo> pageByCondition(Page<YddAdvimg> page, LambdaQueryWrapper<YddAdvimg> queryWrapper) {
        //查询
        this.page(page,queryWrapper);
        //数据拷贝
        Page<YddAdvimgVo> voPage = new Page<>();
        BeanUtils.copyProperties(page,voPage,"records");
        List<YddAdvimgVo> collect = page.getRecords().stream().map((advimg) -> {
            YddAdvimgVo yddAdvimgVo = new YddAdvimgVo();
            BeanUtils.copyProperties(advimg, yddAdvimgVo);
            YddAdvpos advpos = yddAdvposService.getById(yddAdvimgVo.getAdvimgpos());
            yddAdvimgVo.setAdvposname(advpos.getAdvposname());
            return yddAdvimgVo;
        }).collect(Collectors.toList());
        //数据返回
        voPage.setRecords(collect);
        return voPage;
    }
}
